package tv.dyndns.kishibe.server;

import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import tv.dyndns.kishibe.client.packet.PacketThemeModeEditor;
import tv.dyndns.kishibe.client.packet.PacketThemeModeEditor.ThemeModeEditorStatus;
import tv.dyndns.kishibe.client.packet.PacketUserData;
import tv.dyndns.kishibe.server.database.Database;

import com.google.inject.Inject;

public class ThemeModeEditorManager {
	private final Database database;

	@Inject
	public ThemeModeEditorManager(Database database) {
		this.database = database;
	}

	public boolean isThemeModeEditor(int userCode) throws SQLException {
		return database.getThemeModeEditorsStatus(userCode) == ThemeModeEditorStatus.Accepted;
	}

	public void applyThemeModeEditor(int userCode, String text) throws SQLException,
			UnsupportedEncodingException, MessagingException {
		database.updateThemeModeEdtorsStatus(userCode, ThemeModeEditorStatus.Applying);

		// メール送信
		final PacketUserData userData = database.getUserDataByUserCode(userCode);
		final Properties properties = System.getProperties();
		properties.setProperty("mail.smtp.host", "smtpssl.odn.ne.jp");
		properties.setProperty("mail.smtp.port", "465");
		properties.setProperty("mail.smtp.connectiontimeout", "60000");
		properties.setProperty("mail.smtp.timeout", "60000");
		properties.setProperty("mail.smtp.auth", "true");
		properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		properties.setProperty("mail.smtp.socketFactory.fallback", "false");
		properties.setProperty("mail.smtp.socketFactory.port", "465");
		final Session session = Session.getInstance(properties, new javax.mail.Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				try {
					return new PasswordAuthentication("hal25870", database.getPassword("mail"));
				} catch (SQLException e) {
					e.printStackTrace();
					return null;
				}
			}
		});

		MimeMessage mimeMessage = new MimeMessage(session);
		mimeMessage.setFrom(new InternetAddress("hal25870@rio.odn.ne.jp",
				"QMAClone.ThemeModeEditorManager", "iso-2022-jp"));
		mimeMessage.setRecipients(Message.RecipientType.TO, "nodchip@gmail.com");
		mimeMessage.setSubject("[QMAClone]テーマモード編集権限申請 " + userData.playerName, "iso-2022-jp");

		final StringBuilder sb = new StringBuilder();
		sb.append("QMAClone管理者様:\n\n");
		sb.append("テーマモード編集権限の申請がありました。\n");
		sb.append("プレイヤーコード : ").append(userData.userCode).append("\n");
		sb.append("プレイヤー名 : ").append(userData.playerName).append("\n");
		sb.append("プレイ回数 : ").append(userData.playCount).append("\n");
		sb.append("レーティング : ").append(userData.rating).append("\n");
		sb.append("一言 : ").append(text).append("\n");
		sb.append("以上、よろしくお願いいたします。\n\n");
		sb.append("http://kishibe.dyndns.tv/QMAClone/#administratormode\n");

		mimeMessage.setText(sb.toString(), "iso-2022-jp");
		mimeMessage.setHeader("Content-Type", "text/plain");
		mimeMessage.setSentDate(new Date());
		Transport.send(mimeMessage);
	}

	public void acceptThemeModeEditor(int userCode) throws SQLException {
		database.updateThemeModeEdtorsStatus(userCode, ThemeModeEditorStatus.Accepted);
	}

	public void rejectThemeModeEditor(int userCode) throws SQLException {
		database.updateThemeModeEdtorsStatus(userCode, ThemeModeEditorStatus.Refected);
	}

	public List<PacketThemeModeEditor> getThemeModeEditors() throws SQLException {
		return database.getThemeModeEditors();
	}

	public boolean isApplyingThemeModeEditor(int userCode) throws SQLException {
		return database.getThemeModeEditorsStatus(userCode) == ThemeModeEditorStatus.Applying;
	}
}
